package com.takeoutOrder.server.service.impl;

import java.util.List;

import com.takeoutOrder.common.core.domain.AjaxResult;
import com.takeoutOrder.common.core.domain.entity.SysUser;
import com.takeoutOrder.common.exception.ServiceException;
import com.takeoutOrder.common.utils.DateUtils;
import com.takeoutOrder.common.utils.SecurityUtils;
import com.takeoutOrder.system.domain.SysUserRole;
import com.takeoutOrder.system.mapper.SysUserMapper;
import com.takeoutOrder.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.takeoutOrder.server.mapper.ApplyMapper;
import com.takeoutOrder.server.domain.Apply;
import com.takeoutOrder.server.service.IApplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 申请权限Service业务层处理
 *
 * @author linzechen
 * @date 2024-11-27
 */
@Service
public class ApplyServiceImpl implements IApplyService {
    @Autowired
    private ApplyMapper applyMapper;

    /**
     * 查询申请权限
     *
     * @param id 申请权限主键
     * @return 申请权限
     */
    @Override
    public Apply selectApplyById(Long id) {
        return applyMapper.selectApplyById(id);
    }

    /**
     * 查询申请权限列表
     *
     * @param apply 申请权限
     * @return 申请权限
     */
    @Override
    public List<Apply> selectApplyList(Apply apply) {
        return applyMapper.selectApplyList(apply);
    }

    /**
     * 新增申请权限
     *
     * @param apply 申请权限
     * @return 结果
     */
    @Override
    public int insertApply(Apply apply) {
        SysUser sysUser = sysUserMapper.selectUserById(apply.getUserId());
        if (apply.getDeptId()==null||apply.getPhone()==null||apply.getRemake()==null){
            throw new ServiceException("请填写完整申请信息");
        }
        if (sysUser.getDeptId().equals(apply.getDeptId())){
            throw new ServiceException("您已拥有该身份权限,请勿重复申请");
        }
        apply.setCreateTime(DateUtils.getNowDate());
        return applyMapper.insertApply(apply);
    }

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 修改申请权限
     *
     * @param apply 申请权限
     * @return 结果
     */
    @Override
    @Transactional
    public int updateApply(Apply apply) {
        //判断是否为管理员操作,如果不是返回错误
        Long userId = SecurityUtils.getUserId();
        if(!userId.equals(1L)){
            //骗你的,其实根本就没有记录
            throw new ServiceException("您不是管理员,无法修改用户权限,此次操作已被记录");
        }
        apply.setPassTime(DateUtils.getNowDate());
        //修改申请表状态
        int result = applyMapper.updateApply(apply);
        //判断是通过还是拒绝,如果是拒绝,不修改用户表,直接返回结果
        if(apply.getStatus() == 3){
            return result;
        }else {
            //如果是通过,则修改用户表中的部门id,以及修改角色权限user_role表
            Long deptId = apply.getDeptId();
            SysUser sysUser = new SysUser();
            sysUser.setUserId(apply.getUserId());
            sysUser.setDeptId(deptId);
            sysUserMapper.updateUser(sysUser);
            //然后修改user_role表,先取消所有的授权,然后增加新授权,每个人同时只能获得一种授权
            sysUserRoleMapper.deleteUserRoleByUserId(apply.getUserId());
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(apply.getUserId());
            //部门表:110L商家 111用户 112骑手 角色表101L商家 102L骑手 103L用户
            if(deptId.equals(110L)){
                sysUserRole.setRoleId(102L);
            }else if(deptId.equals(112L)){
                sysUserRole.setRoleId(103L);
            }else {
                //申请成为用户,默认就是用户,不出意外这里永远不会被用到
                sysUserRole.setRoleId(104L);
            }
            sysUserRoleMapper.insert(sysUserRole);
        }
        return result;
    }

    /**
     * 批量删除申请权限
     *
     * @param ids 需要删除的申请权限主键
     * @return 结果
     */
    @Override
    public int deleteApplyByIds(Long[] ids) {
        return applyMapper.deleteApplyByIds(ids);
    }

    /**
     * 删除申请权限信息
     *
     * @param id 申请权限主键
     * @return 结果
     */
    @Override
    public int deleteApplyById(Long id) {
        return applyMapper.deleteApplyById(id);
    }
    /**
     * 根据id查询申请表数据
     * @param apply
     * @return
     */
    @Override
    public List<Apply> selectApplyListByUserId(Apply apply) {
        //Long userId = SecurityUtils.getUserId();
        return null;
    }

    /**
     * 用户修改申请表
     * @param apply
     * @return
     */
    @Override
    public int userUpdateApply(Apply apply) {
        if (apply.getStatus()!=0){
            //骗你的,其实根本就没有记录223
            throw new ServiceException("您只能修改未通过审核的申请,还有====>非法操作已被记录");
        }
        return applyMapper.updateApply(apply);
    }
}
